'''
Created on Sep 27, 2012

@author: user
'''
import csv
import commands
import rpy2.robjects as robjects

Number_Features = 200
train = "../../data/lemmaFeaturesTrain_300.csv"
test  = "../../data/lemmaFeaturesTest_300.csv"
trainNew = "../../data/lemmaFeaturesTrain_300_filtered.csv"
testNew  = "../../data/lemmaFeaturesTest_300_filtered.csv"
 


def featureSelection(NUM_FEATURES,trainFeature,testFeature,trainFeatureFiltered,testFeatureFiltered):
    file= open(trainFeature)
    csvReader = csv.reader(file,delimiter = ",")
    lenTrain =0
    for line in csvReader:
        lenTrain = len(line)
        break
    file.close()
    
    label = "V"+str(lenTrain)
    s1 = 'trainDataAll <- read.csv(file="'+trainFeature+'",sep=",", head = FALSE)'
    s2 = 'testDataAll <-   read.csv(file = "'+testFeature +'",sep=",", head = FALSE)'
    s3 = '''
         library(FSelector)
         library(mlbench)
         '''
    s4 = 'weights <- information.gain('+label+ '~., data=trainDataAll)'
    #s4 = 'weights <- random.forest.importance('+label+ '~., data=trainDataAll, importance.type = 1)'
    s5 = 'subSet <- cutoff.k(weights,'+ str(NUM_FEATURES) + ')' 
    s6 = 'testData <- subset(testDataAll, select = subSet)'
    s7 = 'subSet <- c(subSet,"'+label+'")'
    s8 = 'trainData <- subset(trainDataAll,select =subSet)'
    s9 = 'write.table(trainData,file="'+ trainFeatureFiltered +'",row.names=FALSE, col.names=FALSE, sep=",")'
    s10= 'write.table(testData,file="'+ testFeatureFiltered +'",row.names=FALSE, col.names=FALSE, sep=",")'
    robjects.r(s1+"\n"+s2+"\n"+s3+"\n"+s4+"\n"+s5+"\n"+s6+"\n"+s7+"\n"+s8+"\n"+s9+"\n"+s10+"\n")

def convertToSVMtrainType(self,trainFileName):
    tempTrainFile = "../../data/tempTrainFeatures.txt" 
    # Using F-score of svm/tool/fselect.py 
    # 1. Convert file to correct form 
    f1 = open(trainFileName)
    fo = open(tempTrainFile,"w")
    
    csvReader = csv.reader(f1, delimiter = ",")
    csvWriter = csv.writer(fo, delimiter = " ")
    
    for line in csvReader:
        resultLine = []
        resultLine.append(str(line[-1]))
        for i in range(0,len(line)-1):
            value = str(i+1) + ":" + str(line[i])
            resultLine.append(value)
        csvWriter.writerow(resultLine)
            
    fo.close()
    f1.close()
    # 2. Run Feature selection 
#featureSelection(Number_Features,train,test,trainNew,testNew)
